package com.breeze.components.core.exception.error;

import cn.hutool.core.lang.Assert;
import cn.hutool.http.HttpStatus;

/**
 * 错误码集合
 *
 * @author breeze
 */
public enum ErrorCode implements ErrorCodeInterface {

  // common errors
  OK(0, "ok"),
  PARAM_ERROR(HttpStatus.HTTP_BAD_REQUEST, "参数错误"),
  PARAM_ERROR_ARGS(HttpStatus.HTTP_BAD_REQUEST, "参数错误{}"),
  UNAUTHORIZED_ERROR(HttpStatus.HTTP_UNAUTHORIZED, "未经授权"),
  NO_PERMISSION_ERROR(HttpStatus.HTTP_FORBIDDEN, "无权限操作"),
  NOT_FOUND_ERROR(HttpStatus.HTTP_NOT_FOUND, "请求资源不存在"),
  INTERNAL_SERVER_ERROR(HttpStatus.HTTP_INTERNAL_ERROR, "内部服务器错误"),
  PLACEHOLDER(HttpStatus.HTTP_UNAUTHORIZED, "{}"),
  ;

  private final int code;
  private final String msg;

  ErrorCode(int code, String msg) {
    this.code = code;
    this.msg = msg;
  }

  @Override
  public int code() {
    return this.code;
  }

  @Override
  public String message() {
    return this.msg;
  }

  /**
   * 10000~99999 为业务逻辑 错误码 （无代码异常，代码正常流转，并返回提示给用户） 1XX01 XX是代表模块的意思 比如10101 01是Permission模块
   * 错误码的命名最好以模块为开头 比如 NOT_ALLOWED_TO_OPERATE前面加上PERMISSION = PERMISSION_NOT_ALLOWED_TO_OPERATE
   */
  public enum Business implements ErrorCodeInterface {
    // ----------------------------- COMMON --------------------------------------
    OPERATE_FAIL_ERROR(10000, "操作失败, 请稍后重试"),
    UPDATE_ERROR(10001, "更新失败"),
    VALIDATION_ERROR(10002, "验证失败"),
    DATABASE_ERROR(10003, "数据库错误"),
    DATA_NOT_EXIST_ERROR(10004, "数据不存在"),
    DUPLICATE_KEY_EXCEPTION(10005, "已存在记录，请勿重复请求"),
    COMMON_UNSUPPORTED_OPERATION(10006, "不支持的操作"),
    ;

    private final int code;
    private final String msg;

    Business(int code, String msg) {
      this.code = code;
      this.msg = msg;
    }

    @Override
    public int code() {
      return this.code;
    }

    @Override
    public String message() {
      return this.msg;
    }
  }

  /** 1000~9999是外部错误码 比如调用支付失败 */
  public enum External implements ErrorCodeInterface {

    /** 支付宝调用失败 */
    FAIL_TO_PAY_ON_ALIPAY(1001, "支付宝调用失败");

    private final int code;
    private final String msg;

    External(int code, String msg) {
      Assert.isTrue(
          code > 1000 && code < 9999,
          "错误码code值定义失败，External错误码code值范围在1000~9999之间，请查看ErrorCode.External类，当前错误码码为" + name());
      this.code = code;
      this.msg = msg;
    }

    @Override
    public int code() {
      return this.code;
    }

    @Override
    public String message() {
      return this.msg;
    }
  }

  /** 100~999是客户端错误码 客户端如 Web+小程序+手机端 调用出错 可能由于参数问题或者授权问题或者调用过去频繁 */
  public enum Client implements ErrorCodeInterface {
    COMMON_FORBIDDEN_TO_CALL(101, "禁止调用"),

    COMMON_REQUEST_TOO_OFTEN(102, "调用太过频繁"),

    COMMON_REQUEST_PARAMETERS_INVALID(103, "请求参数异常，{}"),

    COMMON_REQUEST_METHOD_INVALID(104, "请求方式: {} 不支持"),

    COMMON_REQUEST_RESUBMIT(105, "请求重复提交"),

    COMMON_NO_AUTHORIZATION(401, "请求接口：{} 失败，用户未授权"),
    REQUEST_NOT_FOUND(404, "请求接口：{} 失败，接口不存在"),
    REPEAT_REQUEST(429, "重复请求"),
    INVALID_TOKEN(107, "token异常"),

    TOKEN_PROCESS_FAILED(108, "token处理失败：{}"),
    ;

    private final int code;
    private final String msg;

    Client(int code, String msg) {
      Assert.isTrue(
          code > 100 && code < 999,
          "错误码code值定义失败，Client错误码code值范围在100~999之间，请查看ErrorCode.Client类，当前错误码码为" + name());
      this.code = code;
      this.msg = msg;
    }

    @Override
    public int code() {
      return this.code;
    }

    @Override
    public String message() {
      return this.msg;
    }
  }

  /** 0~99是内部错误码 例如 框架内部问题之类的 */
  public enum Internal implements ErrorCodeInterface {
    /** 内部错误码 */
    INVALID_PARAMETER(1, "参数异常：{}"),

    /** 该错误主要用于返回 未知的异常（大部分是RuntimeException） 程序未能捕获 未能预料的错误 */
    INTERNAL_ERROR(2, "系统内部错误：{}"),

    GET_ENUM_FAILED(3, "获取枚举类型失败, 枚举类：{}"),

    GET_CACHE_FAILED(4, "获取缓存失败：{}"),

    DB_INTERNAL_ERROR(5, "数据库异常"),

    LOGIN_CAPTCHA_GENERATE_FAIL(7, "验证码生成失败"),

    EXCEL_PROCESS_ERROR(8, "excel处理失败：{}"),
    ;

    private final int code;
    private final String msg;

    Internal(int code, String msg) {
      Assert.isTrue(
          code < 100,
          "错误码code值定义失败，Internal错误码code值范围在100~999之间，请查看ErrorCode.Internal类，当前错误码码为" + name());
      this.code = code;
      this.msg = msg;
    }

    @Override
    public int code() {
      return this.code;
    }

    @Override
    public String message() {
      return this.msg;
    }
  }
}
